var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var md5 = require('md5')
var fs = require("fs")

var conn = mysql.createConnection({
		host:'localhost',
		user:'root',
		password:'123456',
		database: 'projects',
		port: 3306
	});
conn.connect();

router.get('/login', function(req, res, next) {
  	res.render('login');
});

router.post('/dologin', function(req, res, next) {
	req.body.password = md5(req.body.password)
	conn.query('select * from users where username="'+req.body.username+'" and password="'+req.body.password+'"',function(err,rows,fields){	
		if (rows.length==0) {
			res.send('账户和密码不正确');
		}else{
			req.session.is_login = true
			req.session.username = req.body.username
			res.send('ok')
		}
	})
});

router.get('/register', function(req, res, next) {
		res.render("register")
	})

router.post('/getlogin', function(req, res, next) {
	if (req.body.password==req.body.repassword) {
		req.body.password = md5(req.body.password)
	conn.query('insert into users values(null,"'+req.body.username+'","'+req.body.password+'")',function(err,rows,fields){	
		res.redirect("login")
		})
	}else{
		res.redirect("register")
	}
});




module.exports = router;
